package com.wechat.page.chat.message.msg;

import java.io.File;
import java.io.InputStream;

import com.swak.entity.SuccessNewFail;
import com.swak.fx.support.Display;
import com.swak.fx.support.ability.DragAbleAbility;
import com.swak.fx.support.ui.UIImage.ImageCache;
import com.swak.fx.support.ui.UILifecycle;
import com.swak.utils.FileUtils;
import com.swak.utils.IOUtils;
import com.swak.utils.StringUtils;
import com.wechat.dto.MessageDTO;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;

/**
 * 图片消息
 * 
 * @author DELL
 */
public class ImageMsgComp extends FlowPane implements MsgComp, UILifecycle {

	private final String STYLE_CLASS = "message__image";
	private ImageView image = null;

	private final MessageDTO message;

	public ImageMsgComp(MessageDTO message) {
		this.message = message;
		init();
	}

	@Override
	public void initializeParts() {
		getStyleClass().add(STYLE_CLASS);
	}

	@Override
	public void layoutParts() {
		if (this.image == null) {
			return;
		}

		// 设置图标
		this.image.setPreserveRatio(true);
		this.image.setSmooth(true);
		this.image.setCache(true);
		Image img = this.image.getImage();

		// 宽图
		if (img.getWidth() >= img.getHeight()) {
			this.image.setFitWidth(img.getWidth() >= Max_Width ? Max_Width : img.getWidth());
		}

		// 竖图
		else {
			this.image.setFitHeight(img.getHeight() >= Max_Height ? Max_Height : img.getHeight());
		}
		// this.setGraphic(this.image);
		this.getChildren().add(this.image);
	}

	@Override
	public void setupValueChangedListeners() {
		nodeProperty().addListener((ob, o, n) -> {
			if (n instanceof Image) {
				image = new ImageView((Image) n);
			} else if (n instanceof String && StringUtils.startsWithAny((String) n, "http://", "https://")) {
				this.download((String) n);
				return;
			} else if (n instanceof String) {
				image = new ImageView(new Image(FileUtils.in(new File((String) n))));
			} else if (n instanceof ImageView) {
				image = (ImageView) n;
			} else if (n instanceof InputStream) {
				image = new ImageView(new Image((InputStream) n));
				IOUtils.closeQuietly((InputStream) n);
			}
			this.layoutParts();
		});
		prefHeightProperty().addListener((ob, o, n) -> this.layoutParts());
		prefWidthProperty().addListener((ob, o, n) -> this.layoutParts());
	}

	private void download(String url) {
		ImageCache cache = new ImageCache(url);
		cache.successProperty().whenComplete((r, e) -> {
			if (r == SuccessNewFail.Success) {
				Display.runUI(() -> {
					this.nodeProperty().set(cache.openStream());
				});
			}
		});
		cache.start();
	}

	@Override
	public void initializeFinish() {
		// nodeProperty().set(message.getContent());

		// 添加可移动的能力
		new DragAbleAbility(this.image, () -> message);
	}

	private final ObjectProperty<Object> nodeProperty = new SimpleObjectProperty<Object>(this, "image");

	public ObjectProperty<Object> nodeProperty() {
		return nodeProperty;
	}
}
